var app = new Vue({
    el: '#app',
    data() {
        this.num = '111';
        this.chartSettings = {
            stack: { '访问用户': ['独立IP', '总共访问'] },
            area: true
        }
        this.source = {
            dimension: '来源类型',
            metrics: '数量'
        }
        this.bro = {
            dimension: '浏览器',
            metrics: '数量'
        }
        this.os = {
            dimension: '设备',
            metrics: '数量'
        }
        return {
            num: [],
            href: '',
            chartData: {
                columns: ['时间', '独立IP', '总共访问'],
                rows: [
                    { '时间': '周一', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周二', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周三', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周四', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周五', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周六', '独立IP': 0, '总共访问': 0 },
                    { '时间': '周日', '独立IP': 0, '总共访问': 0 },
                ]
            },
            source_data: {
                columns: ['来源类型', '数量'],
                rows: [
                    { '来源类型': '谷歌', '数量': 0 },
                    { '来源类型': '百度', '数量': 0 },
                    { '来源类型': '搜狗', '数量': 0 },
                    { '来源类型': '邮箱', '数量': 0 },
                    { '来源类型': '直接访问', '数量': 0 },
                    { '来源类型': '其他', '数量': 0 },
                ]
            },
            bro_data: {
                columns: ['浏览器', '数量'],
                rows: [
                    { '浏览器': '谷歌', '数量': 0 },
                    { '浏览器': '火狐', '数量': 0 },
                    { '浏览器': '苹果', '数量': 0 },
                    { '浏览器': '微信', '数量': 0 },
                    { '浏览器': '其他', '数量': 0 },
                ]
            },
            os_data: {
                columns: ['设备', '数量'],
                rows: [
                    { '设备': '安卓', '数量': 0 },
                    { '设备': '苹果', '数量': 0 },
                    { '设备': '微软', '数量': 0 },
                    { '设备': '其他', '数量': 0 },
                ]
            },
            city_data: {
                columns: ['位置', '数量'],
                rows: [
                ]
            }
        }
    }
});


// var app1 = new Vue({
//     el: ".app1",
//     data: {
//         num: ''
//     },
//     methods: {

//     }
// });

select_num();
function select_num() {
    axios({
        method: "POST",
        baseURL: "../api/log_sum.php",
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 5000, token: sessionStorage.getItem('token') })
    }).then(function (res) {
        if (res.data.code === 200) {
            app.num = res.data.data;
        } else if (res.data.code === 500) {
            window.location.href = res.data.href;
        } else {
            app.$message.error(res.data.msg);
        };
    }).catch(function (res) {
        console.log(res);
    });
};

select_type()
function select_type() {
    axios({
        method: "POST",
        baseURL: "../api/log_sum.php",
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 5001, token: sessionStorage.getItem('token') })
    }).then(function (res) {
        if (res.data.code === 200) {
            console.log(res.data.data);
            app.bro_data.rows = get_bro(app.bro_data.rows, res.data.data.bro);
            app.source_data.rows = get_source(app.source_data.rows, res.data.data.source);
            app.os_data.rows = get_os(app.os_data.rows, res.data.data.os);

            function get_bro(obj, arr) {
                var len = obj.length;
                var len1 = arr.length;
                for (var i = 0; i < len; i++) {
                    for (var j = 0; j < len1; j++) {
                        if (arr[j].bro === obj[i].浏览器) {
                            obj[i].数量++;
                        };
                    };
                };
                return app.bro_data.rows;
            };

            function get_source(obj, arr) {
                var len = obj.length;
                var len1 = arr.length;
                for (var i = 0; i < len; i++) {
                    for (var j = 0; j < len1; j++) {
                        if (arr[j].source === obj[i].来源类型) {
                            obj[i].数量++;
                        };
                    };
                };
                app.source_data.rows[5].数量 = len1 - (app.source_data.rows[0].数量 + app.source_data.rows[1].数量 + app.source_data.rows[2].数量 + app.source_data.rows[3].数量 + app.source_data.rows[4].数量);
                return app.source_data.rows;
            };

            function get_os(obj, arr) {
                var len = obj.length;
                var len1 = arr.length;
                for (var i = 0; i < len; i++) {
                    for (var j = 0; j < len1; j++) {
                        if (arr[j].os === obj[i].设备) {
                            obj[i].数量++;
                        };
                    };
                };
                app.os_data.rows[3].数量 = len1 - (app.os_data.rows[0].数量 + app.os_data.rows[1].数量 + app.os_data.rows[2].数量);
                return app.os_data.rows;
            };

        } else if (res.data.code === 500) {
            window.location.href = res.data.href;
        } else {
            app.$message.error(res.data.msg);
        };
    }).catch(function (res) {
        console.log(res);
    });
};//总访问量等

select_week();
function select_week() {
    axios({
        method: "POST",
        baseURL: "../api/log_sum.php",
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 5002, token: sessionStorage.getItem('token') })
    }).then(function (res) {
        if (res.data.code === 200) {
            var arr = res.data.data.all;
            var arr_sort = [];
            var arr_ip = res.data.data.ip;
            select_ip();
            function select_ip() {
                for (var i = 0; i < arr.length; i++) {
                    for (var j = 0; j <= arr_sort.length; j++) {
                        if (!arr_sort.includes(arr[i].times.split(' ')[0])) {
                            arr_sort.push(arr[i].times.split(' ')[0]);
                        };
                    };
                };

                for (var i = 0; i < arr_sort.length; i++) {
                    for (var j = 0; j < arr.length; j++) {
                        if (arr_sort[i].includes(arr[j].times.split(' ')[0])) {
                            app.chartData.rows[i].总共访问 += +arr[j].num;
                        };
                    };
                };
            };

            for (var k = 0; k < arr_sort.length; k++) {
                for (l = 0; l < arr_ip.length; l++) {
                    if (arr_ip[l].times.split(' ')[0] === arr_sort[k]) {
                        app.chartData.rows[k].独立IP += +arr_ip[k].num;
                    };
                };
            };//
        } else if (res.data.code === 500) {
            window.location.href = res.data.href;
        } else {
            app.$message.error(res.data.msg);
        };
    }).catch(function (res) {
        console.log(res);
    });
};//周访问量


select_top(1);
function select_top(type, num) {
    var num = num || 10;
    axios({
        method: "POST",
        baseURL: "../api/log_sum.php",
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 5003, token: sessionStorage.getItem('token'), type: type, nums: num })
    }).then(function (res) {
        if (res.data.code === 200) {
            app.href = res.data.data;
        }else if (res.data.code === 500) {
            window.location.href = res.data.href;
        } else {
            app.$message.error(res.data.msg);
        };
    }).catch(function (res) {
        console.log(res);
    });
};//查看最多的页面

select_city();
function select_city() {
    axios({
        method: "POST",
        baseURL: "../api/log_sum.php",
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 5004, token: sessionStorage.getItem('token') })
    }).then(function (res) {
        if (res.data.code === 200) {
            var len = res.data.data.length;
            var arr = res.data.data;
            var arr1 = [];
            var arr2 = [];
            for (var i = 0; i < len; i++) {
                if (!arr1.includes(arr[i].city)) {
                    arr1.push(set_city(arr[i].city));
                    arr2.push({ "位置": set_city(arr[i].city), "数量": 0 });
                };
            };

            for (var i = 0; i < arr1.length; i++) {
                for (var j = 0; j < arr.length; j++) {
                    if (arr1[i] === set_city(arr[j].city)) {
                        arr2[i].数量 += +arr[j].num;
                    };
                };
            };
            city_sort(arr2);//去重
        } else if (res.data.code === 500) {
            window.location.href = res.data.href;
        } else {
            app.$message.error(res.data.msg);
        };
    }).catch(function (res) {
        console.log(res);
    });
};//用户分布

function set_city(obj){
    if(obj && obj.includes('省')){
        var data = '';
        for(var i = 0; i < obj.length - 1; i++){
            data+=obj[i];
        };
        return data;
    }else{
        return obj;
    };
};//兼容框架数据,框架的数据不支持带“省”;

function city_sort(obj){
    var arr = obj || false;
    if(arr){
        var d = [];
        app.city_data.rows = arr.reduce(function(item,next){
            d[next.位置] ? '' : d[next.位置] = true && item.push(next);
            return item;
        },[]);
    };
};//去重
